﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmOutShopBillEdit : Form
    {
        public FrmOutShopBillEdit()
        {
            InitializeComponent();
            gridViewBill.ShowRowIndicator(50);
            gridViewDetail.ShowRowIndicator(50);
        }
        public FrmOutShopBillEdit(int billID)
        {
            InitializeComponent();
            BillEditID = billID;


        }
        private static FrmOutShopBillEdit instance = null;
        public static FrmOutShopBillEdit GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmOutShopBillEdit();

                }
                return instance;
            }
        }
        private int BillEditID = 0;
        OutShopBillBLL _bll = new OutShopBillBLL();
        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            try
            {

                this.Cursor = Cursors.WaitCursor;

                OutProcessShop_Info info = new OutProcessShop_Info();
                if (!string.IsNullOrEmpty(textEditbianhao.Text.Trim()))
                {
                    gridControlBill.DataSource = _bll.GetBill(Convert.ToInt32(textEditbianhao.Text.Trim()), out info);
                    comboBoxOutShop.Text = info.OutShopName;
                    gridControlBill.RefreshDataSource();
                }
                else
                {
                    gridControlBill.DataSource = _bll.GetBill(currShopID, dateEditStartTime.Value.Date, dateEditEndTime.Value.AddDays(1).Date, checkBoxIsCom.Checked);
                    gridControlBill.RefreshDataSource();
                }
            }
            catch (Exception exception)
            {

                MessageBox.Show("数据连接错误，请稍后再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void FrmOutShopBillEdit_Load(object sender, EventArgs e)
        {
            if (BillEditID == 0)
            {

                comboBoxOutShop.DataSource = _bll.GetShopList();
                comboBoxOutShop.DisplayMember = "OutShopName";
                comboBoxOutShop.ValueMember = "OutShopId";

            }
            else
            {
                simpleButtonOK.Enabled = false;
                OutProcessShop_Info info = new OutProcessShop_Info();
                gridControlBill.DataSource = _bll.GetBill(BillEditID, out info);
                comboBoxOutShop.Text = info.OutShopName;
            }
            splitContainerDetail.Panel1Collapsed = true;
        }

        private int currShopID = 0;
        private void comboBoxOutShop_SelectionChangeCommitted(object sender, EventArgs e)
        {
            currShopID = ((OutProcessShop_Info)comboBoxOutShop.SelectedItem).OutShopId;

        }

        private OutShop_Bill_Info CurrBillInfo;
        private void gridViewBill_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                CurrBillInfo = (OutShop_Bill_Info)(gridViewBill.GetRow(e.RowHandle));
                gridControlDetail.DataSource = _bll.GetDetails(CurrBillInfo.BillID);
            }

        }

        private bool isNew = false;
        private bool isUpdata = false;

        private void simpleButtonNew_Click(object sender, EventArgs e)
        {
            isNew = true;
            isUpdata = false;
            this.textEditUnitprice.Text = "";
            textEditSpc.Text = "";
            textEditProductName.Text = "";
            textEditCtmName.Text = "";
            textEditBarCode.Text = "";
            textEditNum.Text = "";
            textEditPname.Text = "";
            checkBoxIsPay.Checked = false;
            checkBoxIsReceive.Checked = false;

            simpleButtonEdit.Enabled = false;
            simpleButtonSave.Enabled = true;
            simpleButtonDel.Enabled = false;
            simpleButtonNew.Enabled = false;
            splitContainerDetail.Panel1Collapsed = false;
        }

        private void simpleButtonEdit_Click(object sender, EventArgs e)
        {
            isNew = false;
            isUpdata = true;
            simpleButtonNew.Enabled = false;
            simpleButtonSave.Enabled = true;
            simpleButtonDel.Enabled = false;
            simpleButtonEdit.Enabled = false;
            splitContainerDetail.Panel1Collapsed = false;
        }

        private OutShopBillDetail_Info CurrDetailInfo;
        private void gridViewDetail_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                CurrDetailInfo = (OutShopBillDetail_Info)(gridViewDetail.GetRow(e.RowHandle));
            }
            this.textEditUnitprice.Text = CurrDetailInfo.UnitPrice.ToString("0.00");
            textEditSpc.Text = CurrDetailInfo.Spc;
            textEditProductName.Text = CurrDetailInfo.OrderProductName;
            textEditCtmName.Text = CurrDetailInfo.CtmName;
            textEditBarCode.Text = CurrDetailInfo.OrderProductCode;
            textEditPname.Text = CurrDetailInfo.ProductName;
            textEditNum.Text = CurrDetailInfo.Num.ToString();
            if (CurrDetailInfo.ProductStatus == (int)OutProcessPayStatus.UnPay)
            {
                checkBoxIsReceive.Checked = false;
            }
            else
            {
                checkBoxIsReceive.Checked = true;
            }


            if (CurrDetailInfo.PayStatus == (int)OutProcessProductStatus.Outting)
            {
                checkBoxIsPay.Checked = false;
            }
            else
            {
                checkBoxIsPay.Checked = true;
            }

        }

        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            if (textEditCtmName.Text.Trim() == "")
            {
                MessageBox.Show("客人名字不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (textEditBarCode.Text.Trim() == "")
            {
                MessageBox.Show("产品条码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (textEditSpc.Text.Trim() == "")
            {
                MessageBox.Show("规格不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (textEditUnitprice.Text.Trim() == "")
            {
                MessageBox.Show("单价不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (textEditNum.Text.Trim() == "")
            {
                MessageBox.Show("数量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (textEditPname.Text.Trim() == "")
            {
                MessageBox.Show("产品类型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            simpleButtonEdit.Enabled = true;
            simpleButtonSave.Enabled = false;
            simpleButtonDel.Enabled = true;
            simpleButtonNew.Enabled = true;
            try
            {


                if (isNew)
                {
                    CurrDetailInfo = new OutShopBillDetail_Info();
                    CurrDetailInfo.CtmName = textEditCtmName.Text.Trim();
                    CurrDetailInfo.OrderProductCode = textEditBarCode.Text.Trim();
                    CurrDetailInfo.Spc = textEditSpc.Text.Trim();
                    CurrDetailInfo.UnitPrice = Convert.ToDecimal(textEditUnitprice.Text.Trim());
                    CurrDetailInfo.Num = Convert.ToInt32(textEditNum.Text.Trim());
                    CurrDetailInfo.OrderProductCode = textEditBarCode.Text.Trim();
                    CurrDetailInfo.OrderProductName = textEditProductName.Text.Trim();
                    CurrDetailInfo.ProductName = textEditPname.Text.Trim();
                    CurrDetailInfo.PayStatus = checkBoxIsPay.Checked ? 1 : 0;
                    CurrDetailInfo.ProductStatus = checkBoxIsReceive.Checked ? 1 : 0;
                    _bll.UpdateBillDetail(CurrBillInfo.BillID, CurrDetailInfo);
                }
                else
                {
                    CurrDetailInfo.CtmName = textEditCtmName.Text.Trim();
                    CurrDetailInfo.OrderProductCode = textEditBarCode.Text.Trim();
                    CurrDetailInfo.Spc = textEditSpc.Text.Trim();
                    CurrDetailInfo.UnitPrice = Convert.ToDecimal(textEditUnitprice.Text.Trim());
                    CurrDetailInfo.Num = Convert.ToInt32(textEditNum.Text.Trim());
                    CurrDetailInfo.OrderProductCode = textEditBarCode.Text.Trim();
                    CurrDetailInfo.PayStatus = checkBoxIsPay.Checked ? 1 : 0;
                    CurrDetailInfo.OrderProductName = textEditProductName.Text.Trim();
                    CurrDetailInfo.ProductName = textEditPname.Text.Trim();
                    CurrDetailInfo.ProductStatus = checkBoxIsReceive.Checked ? 1 : 0;
                    _bll.UpdateBillDetail(this.CurrBillInfo.BillID, CurrDetailInfo);

                }
                //splitContainerDetail.Panel1Collapsed = true;
                splitContainerDetail.Panel1Collapsed = true;
                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                gridViewDetail.RefreshData();
            }
            catch (Exception exception)
            {

                MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void simpleButtonDel_Click(object sender, EventArgs e)
        {
            try
            {

                if (DialogResult.OK == MessageBox.Show("确认删除明细  " + CurrDetailInfo.OrderProductName + "?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    _bll.DeleteDetail(CurrDetailInfo);
                }
            }
            catch (Exception exception)
            {

                MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButtonCancel_Click(object sender, EventArgs e)
        {
            simpleButtonEdit.Enabled = true;
            simpleButtonSave.Enabled = false;
            simpleButtonDel.Enabled = true;
            simpleButtonNew.Enabled = true;
            splitContainerDetail.Panel1Collapsed = true;
        }

        private void gridViewBill_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OutShop_Bill_Info info = (OutShop_Bill_Info)gridViewBill.GetRow(e.RowHandle);
                if (e.Column.Name == "BillPayStatus")
                {
                    switch (info.PayStatus)
                    {
                        case (int)OutProcessPayStatus.UnPay:
                            e.DisplayText = "未付款";
                            break;
                        case (int)OutProcessPayStatus.Part:
                            e.DisplayText = "部分付款";
                            break;
                        case (int)OutProcessPayStatus.Complete:
                            e.DisplayText = "付款完毕";
                            break;
                        default:
                            e.DisplayText = "";
                            break;

                    }
                }
                if (e.Column.Name == "BillProductStatus")
                {
                    switch (info.ProductSratus)
                    {
                        case (int)OutProcessProductStatus.Outting:
                            e.DisplayText = "委外中";
                            break;
                        case (int)OutProcessProductStatus.Received:
                            e.DisplayText = "部分收货";
                            break;
                        case (int)OutProcessProductStatus.Complete:
                            e.DisplayText = "完全收货";
                            break;
                        default:
                            e.DisplayText = "";
                            break;
                    }
                }
            }

        }

        private void gridViewDetail_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OutShopBillDetail_Info info = (OutShopBillDetail_Info)gridViewDetail.GetRow(e.RowHandle);
                if (e.Column.Name == "PayStatus")
                {
                    switch (info.PayStatus)
                    {
                        case (int)OutProcessPayStatus.UnPay:
                            e.DisplayText = "未付款";
                            break;
                        case (int)OutProcessPayStatus.Complete:
                            e.DisplayText = "已付款";
                            break;

                        default:
                            e.DisplayText = "";
                            break;

                    }
                }
                if (e.Column.Name == "ProductStatus")
                {
                    switch (info.ProductStatus)
                    {
                        case (int)OutProcessProductStatus.Outting:
                            e.DisplayText = "委外中";
                            break;
                        case (int)OutProcessProductStatus.Received:
                            e.DisplayText = "已收货";
                            break;

                        default:
                            e.DisplayText = "";
                            break;
                    }
                }
            }
        }

        private void FrmOutShopBillEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }



    }
}
